package com.project.webapp.action.backend.system;

import java.util.List;

import javax.servlet.http.HttpServletRequest;

import org.apache.struts2.ServletActionContext;

import com.project.dao.bean.Limits;
import com.project.dao.bean.Role;
import com.project.dao.bean.Rolelimit;
import com.project.service.backend.basic.LimitService;
import com.project.service.backend.basic.RoleService;
import com.project.service.backend.basic.RolelimitService;
import com.project.webapp.action.BaseAction;

/**
 * 后台角色管理 Action
 * @author Sn
 *
 */
public class RoleAction extends BaseAction 
{

	/**
	 * 序列化版本号
	 */
	private static final long serialVersionUID = 1L;
	private static final String ADD_MENU = "addRole";
	private String operateResult    = "";
	private RoleService roleService;
	private Role role;
	List<Role> roleList;
	private LimitService limitService;
	private RolelimitService rolelimitService;
	List<Limits> limitList;
	
	@Override
	public String execute() throws Exception 
	{
		role = new Role();
		if(roleList!=null && roleList.size()>0)
		{
			roleList.clear();
		}
		return this.query();
	}
	
	public String showAdd() throws Exception 
	{
		role = new Role();
		limitList = limitService.findLimit();
        operateResult = "";
        return SUCCESS;
    }

    /**
     * 
     * <pre>
     * 查询
     * </pre>
     *
     * @return
     */
    public String query() 
    {
        try 
        {
            operateResult = "";
            roleList = roleService.findRoleBypg(role);
            for (Role role : roleList) 
            {
            	role.setLimits(roleService.getRoleLimit(role));
			}
            System.out.println(roleList.size()+"~~~~~~~~~~");
        } catch (Exception e) 
        {
            e.printStackTrace();
        }

        return SUCCESS;
    }
    
    /**
     * 
     * <pre>
     * 删除
     * </pre>
     *
     * @return
     */
    public String delete() 
    {
        HttpServletRequest req = ServletActionContext.getRequest();
        String roleId = req.getParameter("roleId");
        int id = Integer.parseInt(roleId);
        try 
        {
        	role.setRoleId(id);
            
        	roleService.delete(role);
            roleList = roleService.findRoleBypg(role);
            operateResult = "suc";
        } catch (Exception e) 
        {
            e.printStackTrace();
            operateResult = "fail";
        }
        return SUCCESS;
    }
    
    /**
     * 
     * <pre>
     * 编辑
     * </pre>
     *
     * @return
     * @throws Exception 
     */
    public String edit() throws Exception 
    {
        HttpServletRequest req = ServletActionContext.getRequest();
        if (role == null) 
        {
        	role = new Role();
        }
        operateResult = "";
        String roleId = req.getParameter("roleId");
        int id = Integer.parseInt(roleId);
        role.setRoleId(id);
        role = roleService.getRoleById(role);
        List<Limits> lims = roleService.getRoleLimit(role);
        role.setLimits(lims);
        limitList = limitService.findLimit();
        for (Limits lim : lims) 
        {
        	for (Limits limit : limitList) 
        	{
    			if(limit.getLimitId()==lim.getLimitId())
    			{
    				limitList.remove(limit);
    				break;
    			}
    		}
		}
        return SUCCESS;
    }
    
    /**
     * 
     * <pre>
     * 保存
     * </pre>
     *
     * @return
     */
    public String save() 
    {
    	try 
    	{
    		int roleId = roleService.save(role);
    		System.out.println(roleId+"~~~role");
    		Rolelimit rolelimit = new Rolelimit();
    		rolelimit.setRoleId(roleId);
    		int[] popIds = role.getPopIds();
    		for (int i : popIds) 
    		{
    			rolelimit.setLimitId(i);
    			rolelimitService.save(rolelimit);
			}
    		operateResult = "suc";
    		
    	} catch (Exception e) 
    	{
    		e.printStackTrace();
    		operateResult = "fail";
    	}
    	role = new Role();
    	return ADD_MENU;
    }
    
    public String update() 
    {
    	try 
    	{
    		roleService.update(role);
    		Rolelimit rolelimit = new Rolelimit();
    		rolelimit.setRoleId(role.getRoleId());
    		rolelimitService.delete(rolelimit);
    		int[] popIds = role.getPopIds();
    		for (int i : popIds) 
    		{
    			rolelimit.setLimitId(i);
    			rolelimitService.save(rolelimit);
			}
    		operateResult = "suc";
    	} catch (Exception e) 
    	{
    		e.printStackTrace();
    		operateResult = "fail";
    	}
    	role = new Role();
    	return ADD_MENU;
    }

	public String getOperateResult() {
		return operateResult;
	}

	public void setOperateResult(String operateResult) {
		this.operateResult = operateResult;
	}

	public RoleService getRoleService() {
		return roleService;
	}

	public void setRoleService(RoleService roleService) {
		this.roleService = roleService;
	}

	public Role getRole() {
		return role;
	}

	public void setRole(Role role) {
		this.role = role;
	}

	public List<Role> getRoleList() {
		return roleList;
	}

	public void setRoleList(List<Role> roleList) {
		this.roleList = roleList;
	}

	public static long getSerialversionuid() {
		return serialVersionUID;
	}

	public LimitService getLimitService() {
		return limitService;
	}

	public void setLimitService(LimitService limitService) {
		this.limitService = limitService;
	}

	public List<Limits> getLimitList() {
		return limitList;
	}

	public void setLimitList(List<Limits> limitList) {
		this.limitList = limitList;
	}

	public RolelimitService getRolelimitService() {
		return rolelimitService;
	}

	public void setRolelimitService(RolelimitService rolelimitService) {
		this.rolelimitService = rolelimitService;
	}
    
}
